package com.heima.article.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ArticleHomeMapper;
import com.heima.article.service.ApArticleService;
import com.heima.model.article.constant.ArticleConstant;
import com.heima.model.article.dto.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class ArticleServiceImpl extends ServiceImpl<ArticleHomeMapper, ApArticle> implements ApArticleService {
    @Autowired
    private ArticleHomeMapper articleHomeMapper;

    @Override
    public ResponseResult load(Integer loadtype, ArticleHomeDto dto) {
        if (dto == null) {
            log.info("参数错误为空");
            return ResponseResult.errorResult(500, "参数错误");
        }
        if (dto.getSize() == null || dto.getSize() <= 0) {
            dto.setSize(ArticleConstant.DEFAULT_SIZE);
        }
        if (dto.getTag() == null) {
            dto.setTag(ArticleConstant.DEFAULT_TAG);
        }
        if (dto.getMaxBehotTime() == null) {
            dto.setMaxBehotTime(new Date());
        }
        if (dto.getMinBehotTime() == null) {
            dto.setMinBehotTime(new Date());
        }
        List<ApArticle> apArticles = articleHomeMapper.selectArticleHome(dto, loadtype);
        log.info("文章列表加载成功:{}", apArticles);
        return ResponseResult.okResult(apArticles);
    }
}
